package leetcode;

import code.Tree;

/**
 * 描述:617. 合并二叉树
 *
 * @author Madison You
 * @created 10:48
 */
public class Easy_617_MergeTrees {
    public static void main(String[] args) {
        TreeNode treeNode1 = CreateTreeNode.arrayToTreeNode(new Integer[]{1, 3, 2, 5});
        TreeNode treeNode2 = CreateTreeNode.arrayToTreeNode(new Integer[]{2,1,3,null,4,null,7});
        TreeNode treeNode = new Easy_617_MergeTrees().mergeTrees(treeNode1, treeNode2);
        System.out.println(treeNode);
    }

    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        }

        TreeNode root = new TreeNode((root1 == null ? 0 : root1.val) + (root2 == null ? 0 : root2.val));

        root.left = mergeTrees(root1 == null ? null : root1.left, root2 == null ? null : root2.left);
        root.right = mergeTrees(root1 == null ? null : root1.right, root2 == null ? null : root2.right);
        return root;
    }
}
